package com.huike.web.controller.system;

import com.huike.clues.domain.vo.SysRoleVo;
import com.huike.clues.service.ISysRoleService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysRoleDTO;
import com.huike.common.core.domain.entity.SysUserDTO;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.exception.CustomException;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description SysRoleController
 * @Author chenjialiang
 * @Date 2023-10-15
 */
@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "角色相关接口")
public class SysRoleController extends BaseController {

    private final ISysRoleService iSysRoleService;

    /**
     * 新增角色请求
     * @param sysRoleDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增角色接口")
    public AjaxResult addRole(@RequestBody SysRoleDTO sysRoleDTO) {
        log.info("开始执行新增角色接口：{}",sysRoleDTO);
        Map<String, Object> map = new HashMap<>();
        map.put("menuCheckStrictly", 1);
        if (!sysRoleDTO.isMenuCheckStrictly()){
            map.put("menuCheckStrictly", 0);
        }
        map.put("deptCheckStrictly", 1);
        if (!sysRoleDTO.isDeptCheckStrictly()){
            map.put("deptCheckStrictly", 0);

        }

        map.put("roleSort", Integer.parseInt(sysRoleDTO.getRoleSort()));
        sysRoleDTO.setParams(map);
        //TODO dataScope没有怎么处理 remark传没过来

        sysRoleDTO.setCreateBy(SecurityUtils.getUsername());
        sysRoleDTO.setCreateTime(new Date());
        sysRoleDTO.setUpdateBy(SecurityUtils.getUsername());
        sysRoleDTO.setUpdateTime(new Date());
        return toAjax(iSysRoleService.insertUser(sysRoleDTO));
    }

    /**
     * 分页查询
     * @param sysRoleDTO
     * @return
     */
    @ApiOperation("角色分页接口")
    @GetMapping("/list")
    public TableDataInfo list(SysRoleDTO sysRoleDTO) {
        log.info("开始执行角色分页接口：{}", sysRoleDTO);
        startPage();
        List<SysRoleDTO> sysRoleDTOList = iSysRoleService.selectRoleList(sysRoleDTO);
        return getDataTable(sysRoleDTOList);
    }

    /**
     * 根据id查询角色信息
     * @param roleId
     * @return
     */
    @GetMapping("/{roleId}")
    @ApiOperation("id查询角色接口")
    public AjaxResult<SysRoleVo> findByOne(@PathVariable("roleId") Long roleId) {
        log.info("开始执行id查询角色接口：roleId:{}",roleId);
        SysRoleVo sysRoleVo = iSysRoleService.selectByRoleId(roleId);
        return AjaxResult.success("查询成功", sysRoleVo);

    }

    /**
     * 角色修改请求
     * @param sysRoleDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改角色接口")
    public AjaxResult update(@RequestBody SysRoleDTO sysRoleDTO) {
        log.info("开始执行修改角色接口：{}", sysRoleDTO);
        return toAjax(iSysRoleService.updateRoleById(sysRoleDTO));
    }

    /**
     * 状态修改
     * @param sysRoleDTO
     * @return
     */
    @PutMapping("/changeStatus")
    @ApiOperation("角色状态改变接口")
    public AjaxResult changeStatus(@RequestBody SysRoleDTO sysRoleDTO) {
        log.info("开始执行角色状态改变接口：{}",sysRoleDTO);
        return toAjax(iSysRoleService.updateRoleStatusById(sysRoleDTO));
    }

    /**
     * 修改保存数据权限请求
     * @param sysRoleDTO
     * @return
     */
    @ApiOperation("修改保存数据权限接口")
    @PutMapping("/dataScope")
    public AjaxResult updateDataScope(@RequestBody SysRoleDTO sysRoleDTO) {
        log.info("开始执行修改保存数据权限接口：{}", sysRoleDTO);
        return toAjax(iSysRoleService.updateRoleById(sysRoleDTO));
    }

    /**
     * 角色删除请求
     * @return
     */
    @DeleteMapping("/{roleIds}")
    @ApiOperation("角色删除接口")
    public AjaxResult delete(@PathVariable Long[] roleIds) {
        log.info("开始执行角色删除接口：ids:{}", roleIds);
        return toAjax(iSysRoleService.deleteBatch(roleIds));
    }

    @GetMapping("/optionselect")
    @ApiOperation("暂定")
    public AjaxResult optionselect() {
        // todo
        // throw new CustomException("不知道干嘛的  optionselect");
        return AjaxResult.success(iSysRoleService.selectRoleAll());
    }

    @GetMapping("/export")
    public AjaxResult export(SysRoleDTO sysRoleDTO) {
        // todo
        // throw new CustomException("不知道干嘛的  export");
        List<SysRoleDTO> list = iSysRoleService.selectRoleList(sysRoleDTO);
        ExcelUtil<SysRoleDTO> util = new ExcelUtil<SysRoleDTO>(SysRoleDTO.class);
        return util.exportExcel(list, "用户数据");
    }
}
